package com.dz.risk.aspect;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dz.risk.exception.VerifyException;
import com.dz.risk.pojo.domain.ReportFormsEntity;
import com.dz.risk.service.ReportFormsService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 报表基本信息切面处理类
 *
 * @author Doke
 * @date 2021/7/29 17:44
 */
@Component
@Aspect
public class ReportFormsAop {

    private final ReportFormsService reportFormsService;

    @Autowired
    public ReportFormsAop(ReportFormsService reportFormsService) {
        this.reportFormsService = reportFormsService;
    }

    /**
     * 验证新增报表基础信息
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.dz.risk.service.impl.ReportFormsServiceImpl.save(..))")
    public Object checkAdd(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        ReportFormsEntity reportFormsEntity = (ReportFormsEntity) args[0];

        QueryWrapper<ReportFormsEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ReportFormsEntity::getReportFormsSerialNumber, reportFormsEntity.getReportFormsSerialNumber())
                .or()
                .eq(ReportFormsEntity::getReportFormsName, reportFormsEntity.getReportFormsName());
        List<ReportFormsEntity> reportFormsEntities = reportFormsService.list(wrapper);
        if (!CollectionUtils.isEmpty(reportFormsEntities)) {
            throw new VerifyException("报表名称/报表编号已存在");
        }

        return point.proceed();
    }

    /**
     * 验证更新报表基础信息
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.dz.risk.service.impl.ReportFormsServiceImpl.update(..))")
    public Object checkUpdate(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        ReportFormsEntity reportFormsEntity = (ReportFormsEntity) args[0];

        QueryWrapper<ReportFormsEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .ne(ReportFormsEntity::getId, reportFormsEntity.getId())
                .eq(ReportFormsEntity::getReportFormsName, reportFormsEntity.getReportFormsName());

        List<ReportFormsEntity> reportFormsEntities = reportFormsService.list(wrapper);
        if (!CollectionUtils.isEmpty(reportFormsEntities)) {
            throw new VerifyException("报表名称已存在");
        }

        return point.proceed();
    }

    /**
     * 验证生成新版报表时版本是否重复
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.dz.risk.service.impl.ReportFormsServiceImpl.generateNewVersionReportFormsByReportFormsId(..))")
    public Object checkGenerateNewVersionReportForms(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        ReportFormsEntity reportFormsEntity = (ReportFormsEntity) args[0];

        //验证版本是否重叠
        checkVersionOverlapping(reportFormsEntity);

        return point.proceed();
    }

    /**
     * 验证版本是否重叠
     *
     * @param reportFormsEntity 报表基本信息实体
     */
    private void checkVersionOverlapping(ReportFormsEntity reportFormsEntity) {
        //根据报表编号以及版本号查询报表基本信息
        List<ReportFormsEntity> dataBaseReportForms = reportFormsService.selectByReportFormsNameAndVersion(reportFormsEntity.getReportFormsSerialNumber(), reportFormsEntity.getBeginDate(), reportFormsEntity.getEndDate());

        if (!CollectionUtils.isEmpty(dataBaseReportForms)) {
            throw new VerifyException("该报表已存在包含此开始时间或结束时间区间的版本");
        }

    }
}
